package top.zhangjianyong.tools.factory;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import top.zhangjianyong.tools.entity.AliBill;
import top.zhangjianyong.tools.entity.Bill;
import top.zhangjianyong.tools.entity.WaBill;
import top.zhangjianyong.tools.entity.WxBill;

import java.math.BigDecimal;

/**
 * @author ： zhangjianyong
 * @date ： 2022/7/20 4:24 PM
 */
@Slf4j
public class WaBillFactory {

    public static WaBill create() {
        return new WaBill();
    }

    public static WaBill create(AliBill aliBill) {
        WaBill waBill = create();
        BeanUtils.copyProperties(aliBill, waBill);
        waBill.setPlatform("支付宝");
        return waBill;
    }

    public static WaBill create(WxBill wxBill) {
        WaBill waBill = create();
        BeanUtils.copyProperties(wxBill, waBill);
        waBill.setPlatform("微信");
        return waBill;
    }

    public static <T extends Bill> WaBill create(T t, String platform) {
        WaBill waBill = create();
        BeanUtils.copyProperties(t, waBill);
        waBill.setPlatform(platform);
        /*退款单号*/
        if ("退款成功".equals(waBill.getState())) {
            String orderNo = waBill.getOrderNo();
            if (orderNo.contains("_")) {
                waBill.setOriginOrderNo(orderNo.split("_")[0]);
            } else if (orderNo.contains("*")) {
                waBill.setOriginOrderNo(orderNo.split("\\*")[0]);
            }
        }
        //修改订单状态
        switch (platform) {
            case "支付宝":
                /*修改支付宝状态*/
                setAliState(waBill);
                /*修改收支标记*/
                setAliInOrOut(waBill);
                break;
            case "微信":
                setWxState(waBill);
                break;
            default:
                throw new RuntimeException("平台错误");

        }


        return waBill;
    }

    private static void setAliInOrOut(WaBill waBill) {
        String inOrOut = waBill.getInOrOut();
        String state = waBill.getState();
        String type = waBill.getType();
        String payWay = waBill.getPayWay();
        if ("不计收支".equals(inOrOut) && "成功".equals(state)) {
            /*余额宝收入*/
            if ("投资理财".equals(type) && "余额宝".equals(payWay)) {
                waBill.setInOrOut("收入");
                return;
            }
            /*亲情卡*/
            if (payWay.startsWith("亲情卡")) {
                waBill.setInOrOut("支出");
                return;
            }
            /*借充电宝*/
            if ("生活服务".equals(type) || "信用借还".equals(type)) {
                waBill.setState("关闭");
                return;
            }
            throw new RuntimeException("无法确定的收支状态：" + JSONObject.toJSONString(waBill));
        }
    }

    private static void setWxState(WaBill waBill) {
        /*检查状态*/
        String state = waBill.getState();
        switch (state) {
            case "已转账"://扫二维码付款
            case "支付成功"://商户消费，微信红包
            case "已收钱"://二维码收款
            case "充值成功"://彩票入账
                waBill.setState("成功");
                break;
            case "充值完成"://转入零钱
            case "对方已收钱"://转账
            case "已存入零钱"://转账红包
            case "已全额退款"://退款
                waBill.setState("关闭");
                break;
            default:
                log.info("请检查订单状态:{}", JSONObject.toJSONString(waBill));
                throw new RuntimeException("请检查订单状态：" + state);
        }
        /*确定收支状态*/
        state=waBill.getState();
        String type = waBill.getType();
        String inOrOut = waBill.getInOrOut();

        if ("/".equals(inOrOut) && "成功".equals(state)) {
            if ("转入零钱通-来自零钱".equals(type)) {
                waBill.setState("关闭");
                return;
            }
            throw new RuntimeException("未知的收支状态:" + JSONObject.toJSONString(waBill));
        }
    }

    private static void setAliState(WaBill waBill) {
        /*检查状态*/
        String state = waBill.getState();
        switch (state) {
            case "交易成功":
            case "支付成功":
            case "解冻成功":
            case "信用服务使用成功":
                waBill.setState("成功");
                break;
            case "退款成功":
            case "交易关闭":
                waBill.setState("关闭");
                break;
            case "等待确认收货":
                waBill.setState("处理中");
                break;
            default:
                log.info("请检查订单状态:{}", JSONObject.toJSONString(waBill));
                throw new RuntimeException("请检查订单状态：" + state);
        }
        //余额宝转入转出关闭
        String type = waBill.getType();
        String inOrOut = waBill.getInOrOut();
        String counterparty = waBill.getCounterparty();
        String counterpartyAccount = waBill.getCounterpartyAccount();
        String commodity = waBill.getCommodity();
        if ("投资理财".equals(type) && "不计收支".equals(inOrOut) && "余额宝".equals(counterparty)
                && "fun***@thfund.com.cn".equals(counterpartyAccount)) {
            if ("余额宝-钱管家转入".equals(commodity)
                    || "转账收款到余额宝".equals(commodity)
                    || "余额宝-自动转入".equals(commodity)) {
                waBill.setState("关闭");
            }
        }

    }

    public static void main(String[] args) {
        String s = "/";
        System.out.println(s.equals("/"));
        String[] split = s.split("\\*");
        System.out.println(split[0]);
    }

}
